/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import java.math.BigDecimal;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.AspectId;
import ru.irenproject.store.Ids;
import ru.irenproject.store.Initializer;
import ru.irenproject.store.Thing;

import com.google.common.base.CharMatcher;
import com.google.protobuf.ByteString;

public final class Utils {
  public static List<ByteString> getThingIds(Iterable<? extends Thing> things) {
    List<ByteString> res = new ArrayList<>();
    for (Thing thing : things) {
      res.add(thing.id());
    }
    return res;
  }

  public static <T extends AbstractThing> T requireAspect(Thing aspectHost, AspectId aspectId, Class<T> targetClass,
      Initializer<? super T> initializer) {
    T res;
    ByteString id = aspectHost.getAspect(aspectId);
    if (id == null) {
      res = aspectHost.store().create(targetClass);
      initializer.initialize(res);
      aspectHost.setAspect(aspectId, res.id());
    } else {
      res = aspectHost.store().get(id, targetClass);
    }
    return res;
  }

  public static String shortenString(String s, int softLimit, int hardLimit) {
    final String ELLIPSIS = "...";
    String res;

    if (s.length() <= softLimit) {
      res = s;
    } else {
      int k = softLimit;
      int e = Math.min(s.length(), hardLimit);
      while ((k < e) && !Character.isWhitespace(s.charAt(k))) {
        ++k;
      }
      if (k < e) {
        res = s.substring(0, k) + ELLIPSIS;
      } else if (s.length() <= hardLimit) {
        res = s;
      } else {
        int hardBreak = hardLimit;
        if (Character.isLowSurrogate(s.charAt(hardLimit))) {
          --hardBreak;
        }
        res = s.substring(0, hardBreak) + ELLIPSIS;
      }
    }

    return res;
  }

  public static boolean removeAll(Collection<?> from, Iterable<?> itemsToRemove) {
    boolean res = false;
    for (Object item : itemsToRemove) {
      res |= from.remove(item);
    }
    return res;
  }

  public static BigDecimal parseNormalizedDecimal(String s) {
    TestCheck.input(s.length() == 6);
    TestCheck.input(s.charAt(1) == '.');
    TestCheck.input(CharMatcher.inRange('0', '9').countIn(s) == s.length() - 1);

    BigDecimal res = new BigDecimal(s);
    TestCheck.input(res.compareTo(BigDecimal.ONE) <= 0);
    return res;
  }

  public static Path getStoreDirectory(ByteString storeId) {
    return DataDirectory.get()
        .resolve("openFiles")
        .resolve(Ids.toHexString(storeId));
  }

  private Utils() {}
}
